|
Download
FAQ History |
|
API
Search Feedback |
Creating a Web Service with JAX-RPC
This section shows how to build and deploy a simple Web service called
MyHelloService. A later section, Creating Web Service Clients with JAX-RPC, provides examples of JAX-RPC clients that access this service. The source code required byMyHelloServiceis in<INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/.These are the basic steps for creating the service:
The sections that follow cover these steps in greater detail. Before proceeding, you should try out the examples in the Getting Started with Web Applications chapter. Make sure that you've followed the instructions in Setting Up To Build and Deploy Tutorial Examples.
Coding the Service Endpoint Interface and Implementation Class
A service endpoint interface declares the methods that a remote client may invoke on the service. In this example, the interface declares a single method named
sayHello.A service endpoint interface must conform to a few rules:
- It extends the
java.rmi.Remoteinterface.- It must not have constant declarations, such as
public final static.- The methods must throw the
java.rmi.RemoteExceptionor one of its subclasses. (The methods may also throw service-specific exceptions.)- Method parameters and return types must be supported JAX-RPC types. See the section Types Supported By JAX-RPC.
In this example, the service endpoint interface is
HelloIF.java:package helloservice; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloIF extends Remote { public String sayHello(String s) throws RemoteException; }In addition to the interface, you'll need the class that implements the interface. In this example, the implementation class is called
HelloImpl:package helloservice; public class HelloImpl implements HelloIF { public String message ="Hello"; public String sayHello(String s) { return message + s; } }Building the Service
To build
MyHelloService, in a terminal window go to the<INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/directory and type the following:The preceding command executes three
asanttasks:compile-service
This
asanttask compilesHelloIF.javaandHelloImpl.java, writing the class files to thebuildsubdirectory.generate-wsdl
The
generate-wsdltask runs thewscompiletool, which creates theMyHelloService.wsdlfile in thebuilddirectory. Thegenerate-wsdltask runswscompileas follows:The
-defineflag instructs the tool to read the service endpoint interface and to create a WSDL file. The-dand-ndflags tell the tool to write output to thebuildsubdirectory. The tool reads the followingconfig-interface.xmlfile:<?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <service name="MyHelloService" targetNamespace="urn:Foo" typeNamespace="urn:Foo" packageName="helloservice"> <interface name="helloservice.HelloIF"/> </service> </configuration>The
config.xml-interfacefile tellswscompileto create a WSDL file with the following information:
- The service name is
MyHelloService.- The WSDL namespace is
urn:Foo. (To understand this namespace, you need to be familiar with WSDL technology. See Further Information)- The classes for the
MyHelloServiceare in thehelloservicepackage.- The service endpoint interface is
helloservice.HelloIF.generate-mapping
This
asanttask also runswscompile, this time to create amapping.xmlfile and a set of runtime classes. Themapping.xmlfile maps the package names of the service classes to a namespace URI of the WSDL file. The set of runtime classes is made up of serializers, deserializers, and ties. Thegenerate-mapingtask runswscompileas follows:The
-genflag instructs the tool to read the WSDL file and to create the runtime classes. The-dand-ndflags tell the tool to write output to thebuildsubdirectory, and the-mappingflag specifies the mapping file. The tool reads the followingconfig-wsdl.xmlfile:<?xml version="1.0" encoding="UTF-8"?> <configuration xmlns="http://java.sun.com/xml/ns/jax-rpc/ri/config"> <wsdl location="build/MyHelloService.wsdl" packageName="helloservice"/> </configuration>Packaging the Service
Behind the scenes, a JAX-RPC Web service is actually a servlet. Because a servlet is a Web component, to package the service you run the New Web Component wizard of the
deploytoolutility. During this process the wizard performs the following tasks.To start the New Web Component wizard, select File
New
Web Application WAR. The wizard displays the following dialog boxes.
- Introduction dialog box
- WAR File dialog box
- Select the button labelled Create New Stand-Alone WAR Module.
- In the WAR Location field, enter
<INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/MyHelloService.war.- In the WAR Display Field, enter
MyHelloService.- Click Edit.
- In the tree under Available Files, locate the
<INSTALL>/j2eetutorial14/examples/jaxrpc/helloservice/directory.- Select the
buildsubdirectory.- Click Add.
- Click OK.
- Click Next.
- Choose Component Type dialog box
- Choose Service dialog box
- Web Service Endpoint dialog box
- Component General Properties dialog box
Specifying the Endpoint Address
To access
MyHelloService, the tutorial clients will specify this service endpoint address URI:The
/hello-jaxrpcstring is the context root of the servlet that implements MyHelloService. The/hellostring is the servlet alias. To specify the endpoint address, you set the context root and alias as follows:Bug 4866894: Do not enter the Service Endpoint Address URI in the Endpoint tab of
HelloImpl.Deploying the Service
In deploytool, perform these steps:
Now you are ready to create a client that accesses this service.
|
Download
FAQ History |
|
API
Search Feedback |
All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.